package com.why.catchMsg.util;

import com.arronlong.httpclientutil.HttpClientUtil;
import com.arronlong.httpclientutil.builder.HCB;
import com.arronlong.httpclientutil.common.HttpConfig;
import com.arronlong.httpclientutil.common.HttpHeader;
import com.arronlong.httpclientutil.exception.HttpProcessException;
import org.apache.http.Header;
import org.apache.http.client.HttpClient;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.HashMap;
import java.util.Map;

public class HttpClientUtils {

    public static  String stdRequest(String url,RequestMethod requestMethod,Map<String,Object> param)  {
        String sessionId=String.valueOf(param.get("session_id"));
        Header[] headers = HttpHeader.custom()
                .other("session_id",sessionId).build();//.contentType("application/json")
        param.remove("session_id");



        HCB hcb 				= null                    //重试5次
                ;
        try {
            hcb = HCB.custom()
                    .pool(100, 10)    	//启用连接池，每个路由最大创建10个链接，总连接数限制为100个
    //                .sslpv(SSLs.SSLProtocolVersion.TLSv1_2) 	//可设置ssl版本号，默认SSLv3，用于ssl，也可以调用sslpv("TLSv1.2")
    //                .ssl()  			   		//https，支持自定义ssl证书路径和密码，ssl(String keyStorePath, String keyStorepass)
                    .retry(5);
        } catch (HttpProcessException e) {
            e.printStackTrace();
        }
        HttpClient client = hcb.build();

        HttpConfig config = HttpConfig.custom()
                .headers(headers)	//设置headers，不需要时则无需设置
                .timeout(1000) 		//超时
                .url(url)           //设置请求的url
                .map(param)			//设置请求参数，没有则无需设置
                .encoding("utf-8")  //设置请求和返回编码，默认就是Charset.defaultCharset()
                .client(client);

        String result = null;    //get请求
        try {
            switch (requestMethod){
                case GET:
                    //get请求用不了对应 map方法传参
                    if(!param.isEmpty()){
                        url+="?";
                    }
                    for (Map.Entry<String,Object> entry : param.entrySet()) {
                        url=url+entry.getKey()+"="+entry.getValue()+"&";
                    }
                    if(!param.isEmpty()) {
                        url = url.substring(0, url.length() - 1);
                    }
                    config.url(url);
                    result = HttpClientUtil.get(config);
                    break;
                case POST:
                    result = HttpClientUtil.post(config);
                    break;
                case DELETE:
                    //get请求用不了对应 map方法传参
                    if(!param.isEmpty()){
                        url+="?";
                    }
                    for (Map.Entry<String,Object> entry : param.entrySet()) {
                        url=url+entry.getKey()+"="+entry.getValue()+"&";
                    }
                    if(!param.isEmpty()) {
                        url = url.substring(0, url.length() - 1);
                    }
                    config.url(url);
                    result = HttpClientUtil.delete(config);
                    break;
                case PATCH:
                    result = HttpClientUtil.patch(config);
                    break;
                case PUT:
                    result = HttpClientUtil.put(config);
                    break;
                case HEAD:
                    result = HttpClientUtil.head(config);
                    break;
                case TRACE:
                    result = HttpClientUtil.trace(config);
                    break;
                case OPTIONS:
                    result = HttpClientUtil.options(config);
                    break;
            }
        } catch (HttpProcessException e) {
            e.printStackTrace();
        }
        return result;
    }


    public static  String stdRequestBody(String sessionId,String url,RequestMethod requestMethod,String requestBody) {
        Header[] headers = HttpHeader.custom().contentType("application/json").other("session_id",sessionId).build();

        HCB hcb = null                    //重试5次
                ;
        try {
            hcb = HCB.custom()
                    .pool(100, 10)
                    .retry(5);
        } catch (HttpProcessException e) {
            e.printStackTrace();
        }
        HttpClient client = hcb.build();

        HttpConfig config = HttpConfig.custom()
                .headers(headers)	//设置headers，不需要时则无需设置
                .timeout(300000) 		//超时
                .url(url)           //设置请求的url
                .json(requestBody)
                .encoding("utf-8")  //设置请求和返回编码，默认就是Charset.defaultCharset()
                .client(client);

        String result = null;    //get请求
        try {
            switch (requestMethod){
                case GET:
                    result = HttpClientUtil.get(config);
                    break;
                case POST:
                    result = HttpClientUtil.post(config);
                    break;
                case DELETE:
                    result = HttpClientUtil.delete(config);
                    break;
                case PATCH:
                    result = HttpClientUtil.patch(config);
                    break;
                case PUT:
                    result = HttpClientUtil.put(config);
                    break;
                case HEAD:
                    result = HttpClientUtil.head(config);
                    break;
                case TRACE:
                    result = HttpClientUtil.trace(config);
                    break;
                case OPTIONS:
                    result = HttpClientUtil.options(config);
                    break;
            }
        } catch (HttpProcessException e) {
            e.printStackTrace();
        }
        return result;
    }


}